import socket
import sys
import time
# import tcp4
import json
import struct

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# host=socket.gethostname()#获取本地主机名
host = "127.0.0.1"
port = 8887
# 绑定端口号
serversocket.bind((host, port))
# a = tcp4.tcp4()
# c = json.loads(a)
# print(type(a))

# 设置最大连接数
serversocket.listen(5)
client, addr = serversocket.accept()
while True:
    print('链接地址：%s' % str(addr))
    # while True:
    # try:
    #     data=clientsocket.recv(1024)
    # except Exception:
    #     print('断开的客户端：',addr)
    #     break
    # print(data)
    data = client.recv(1024)
    # data = json.loads(re)
    int_size = struct.calcsize("I")
    (i,), data = struct.unpack("I", data[:int_size]), data[int_size:]
    print(data)
    k = data.decode()
    print(k)
    try:
        t = json.loads(k)
    except Exception:
        continue
    print(t)


serversocket.closel()
